﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using UnityScriptEngine;

namespace EnrollmentSystem
{
    public partial class frmMain : Form
    {
        public frmMain()
        {
            InitializeComponent();

            this.Resize += new EventHandler(frmMain_Resize);
            this.FormClosing += new FormClosingEventHandler(frmMain_FormClosing);
            this.Load += new EventHandler(frmMain_Load);
            sldMain.ItemRemoved += new EventHandler(sldMain_ItemRemoved);
            sldMain.ItemAdded += new EventHandler(sldMain_ItemAdded);
            sldMain.AnimationCompleted += new EventHandler(sldMain_AnimationCompleted);
            mnuCloseAll.Click += new EventHandler(mnuCloseAll_Click);

            //Menus
            mnuAbout.Click += new EventHandler(mnuAbout_Click);
            mnuExit.Click += new EventHandler(mnuExit_Click);
            mnuSettings.Click += new EventHandler(mnuSettings_Click);
        
            DoEvents();
        }
        
        void DoEvents()
        {
            this.Shown += new EventHandler(delegate
            {
                ProcessLogin();
            });
            mnuLogin.Click += new EventHandler(delegate
            {
                ProcessLogin();
            });
            mnuUserManager.Click += new EventHandler(delegate
            {
                bool found = false;
                for (int i = 0; i < sldMain.ItemList.Length; i++)
                {
                    if (sldMain.ItemList[i].Equals("User Manager")) found = true;
                }
                frmUserManager frm = new frmUserManager();
                frm.Initialize("User Manager");
                sldMain.AddItem(frm.PanelForm, "User Manager");
            });
            mnuMasterist.Click += new EventHandler(delegate
            {
                bool found = false;
                for (int i = 0; i < sldMain.ItemList.Length; i++)
                {
                    if (sldMain.ItemList[i].Equals("Master List Manager")) found = true;
                }
                frmMasterist frm = new frmMasterist();
                frm.Initialize("Master List Manager");
                sldMain.AddItem(frm.PanelForm, "Master List Manager");
            });
            mnuGradesManage.Click += new EventHandler(delegate
            {
                bool found = false;
                for (int i = 0; i < sldMain.ItemList.Length; i++)
                {
                    if (sldMain.ItemList[i].Equals("Grades Management")) found = true;
                }
                frmGradesManagement frm = new frmGradesManagement();
                frm.Initialize("Grades Management");
                sldMain.AddItem(frm.PanelForm, "Grades Management");
            });
            mnuEnrollmentReport.Click += new EventHandler(delegate
            {
                frmEnrollmentReport frm = new frmEnrollmentReport();
                frm.Initialize("Enrollment Report Manager");
                sldMain.AddItem(frm.PanelForm, "Enrollment Report Manager");
            });
            mnuOTRManager.Click += new EventHandler(delegate
            {
                frmOTRManager frm = new frmOTRManager();
                frm.Initialize("OTR Manager");
                sldMain.AddItem(frm.PanelForm, "OTR Manager");
            });
            mnuStatementOfAccount.Click += new EventHandler(delegate
            {
                frmStatementOfAccountViewer frm = new frmStatementOfAccountViewer();
                frm.Initialize("Statement of Account Manager");
                sldMain.AddItem(frm.PanelForm, "Statement of Account Manager");
            });
            mnuPaymentEntry.Click += new EventHandler(delegate 
            {
                frmPaymentEntry frm = new frmPaymentEntry();
                frm.Initialize("Payment Manager");
                sldMain.AddItem(frm.PanelForm, "Payment Manager");
            });
            mnuCashDenomination.Click += new EventHandler(delegate
            {
                frmCashDenomination frm = new frmCashDenomination();
                frm.Initialize("Cash Denomination Manager");
                sldMain.AddItem(frm.PanelForm, "Cash Denomination Manager");
            });
            mnuLookUp.Click += new EventHandler(delegate
            {
                bool found = false;
                for (int i = 0; i < sldMain.ItemList.Length; i++)
                {
                    if (sldMain.ItemList[i].Equals("Look Up Manager")) found = true;
                }
                if (!found)
                {
                    frmLookUpManager frm = new frmLookUpManager();
                    frm.Initialize("Look Up Manager");
                    sldMain.AddItem(frm.PanelForm, "Look Up Manager");
                }
            });
            mnuStudentRegistration.Click += new EventHandler(delegate
            {
                bool found = false;
                for (int i = 0; i < sldMain.ItemList.Length; i++)
                {
                    if (sldMain.ItemList[i].Equals("Student Profile Management")) found = true;
                }
                if (!found)
                {
                    frmStudentProfileManager frm = new frmStudentProfileManager();
                    frm.Initialize("Student Profile Management");
                    sldMain.AddItem(frm.PanelForm, "Student Profile Management");
                }
            });
            mnuCourseEnlistment.Click += new EventHandler(delegate
            {
                bool found = false;
                for (int i = 0; i < sldMain.ItemList.Length; i++)
                {
                    if (sldMain.ItemList[i].Equals("Course Enlistment")) found = true;
                }
                if (!found)
                {
                    frmCourseEnlistment frm = new frmCourseEnlistment();
                    frm.Initialize("Course Enlistment");
                    sldMain.AddItem(frm.PanelForm, "Course Enlistment");
                }
            });
            mnuEnrollment.Click += new EventHandler(delegate
            {
                bool found = false;
                for (int i = 0; i < sldMain.ItemList.Length; i++)
                {
                    if (sldMain.ItemList[i].Equals("Enrollment Manager")) found = true;
                }
                if (!found)
                {
                    frmEnrollmentManager frm = new frmEnrollmentManager();
                    frm.Initialize("Enrollment Manager");
                    sldMain.AddItem(frm.PanelForm, "Enrollment Manager");
                }
            });
        }

        void mnuSettings_Click(object sender, EventArgs e)
        {
            bool found = false;
            for (int i = 0; i < sldMain.ItemList.Length; i++)
            {
                if (sldMain.ItemList[i].Equals("Settings")) found = true;
            }
            if (!found)
            {
                frmSettings frm = new frmSettings();
                frm.Initialize("Settings");
                sldMain.AddItem(frm.PanelForm, "Settings");
            }
        }
        void mnuAbout_Click(object sender, EventArgs e)
        {
            frmAboutThis frm = new frmAboutThis();
            frm.Initialize("About Enrollment System");
            sldMain.AddItem(frm.PanelForm, "About Enrollment System");
        }
        void mnuExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        void frmMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (sldMain.ItemCount > 0)
            {
                string msg = string.Format("There are {0} session(s) running. Are you sure to close the application?", sldMain.ItemCount);
                DialogResult dlg = MessageBox.Show(msg, "Exit", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation);
                if (dlg == DialogResult.No) e.Cancel = true;
            }
        }
        void frmMain_Resize(object sender, EventArgs e)
        {
            sldMain.Width = this.Width - 40;
            sldMain.Height = this.Height - 77;
        }
        void sldMain_ItemRemoved(object sender, EventArgs e)
        {
            string[] buf = sldMain.ItemList;
            mnuWindow.DropDownItems.Clear();
            for (int i = 0; i < buf.Length; i++)
            {
                mnuWindow.DropDownItems.Add((i + 1) + "-" + buf[i], null, new EventHandler(window_Click));
                mnuWindow.DropDownItems[i].ForeColor = Color.Black;
            }
            if (sldMain.ItemCount > 0) mnuWindow.DropDownItems[sldMain.SelectedIndex].ForeColor = Color.RoyalBlue;
            mnuCloseAll.Text = "Close All Sessions (" + buf.Length + ")";
        }
        void sldMain_ItemAdded(object sender, EventArgs e)
        {
            string[] buf = sldMain.ItemList;
            mnuWindow.DropDownItems.Clear();
            for (int i = 0; i < buf.Length; i++)
            {
                mnuWindow.DropDownItems.Add((i + 1) + "-" + buf[i], null, new EventHandler(window_Click));
                mnuWindow.DropDownItems[i].ForeColor = Color.Black;
            }
            mnuWindow.DropDownItems[sldMain.SelectedIndex].ForeColor = Color.RoyalBlue;
            mnuCloseAll.Text = "Close All Sessions (" + buf.Length + ")";
        }
        void sldMain_AnimationCompleted(object sender, EventArgs e)
        {
            for (int i = 0; i < mnuWindow.DropDownItems.Count; i++)
            {
                mnuWindow.DropDownItems[i].ForeColor = Color.Black;
            }
            mnuWindow.DropDownItems[sldMain.SelectedIndex].ForeColor = Color.RoyalBlue;
        }
        void window_Click(object sender, EventArgs e)
        {
            int index = mnuWindow.DropDownItems.IndexOf((ToolStripItem)sender);
            for (int i = 0; i < mnuWindow.DropDownItems.Count; i++)
            {
                mnuWindow.DropDownItems[i].ForeColor = Color.Black;
            }
            sldMain.MoveTo(index);
            mnuWindow.DropDownItems[index].ForeColor = Color.RoyalBlue;
        }
        void mnuCloseAll_Click(object sender, EventArgs e)
        {
            if (sldMain.ItemCount > 0)
            {
                sldMain.Controls.Clear();
            }
        }

        void frmMain_Load(object sender, EventArgs e)
        {
            //Load and apply settings
            try
            {
                frmSettings.LoadSettings();
                if (frmSettings.setPref[0].ToString() == "True") UnityScriptEngine.Dev.Debug = true;
                if (frmSettings.setPref[1].ToString() == "True") this.WindowState = FormWindowState.Maximized;
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error loading settings!");
            }
        }

        UnityElement userData = new UnityElement(new object[] { });
        void ProcessLogin()
        {
            if (userData.Data_FieldCount > 1)
            {
                DialogResult res = MessageBox.Show("Logoff " + userData.Data_GetField(1) + "?", "Logoff", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (res == System.Windows.Forms.DialogResult.Yes)
                {
                    userData = new UnityElement(new object[] { 0 });
                    mnuRegs.Enabled = false;
                    mnuPlanning.Enabled = false;
                    mnuAccount.Enabled = false;
                    mnuLookUp.Enabled = false;
                    mnuUserManager.Enabled = false;
                    mnuLogin.Text = "Login";
                }
                else
                {
                    return;
                }
                
            }
            frmLogin frm = new frmLogin();
            DialogResult resdlg = frm.ShowDialog();
            if (resdlg == System.Windows.Forms.DialogResult.OK)
            {
                //Top Level
                userData = frm.UserDetail;
                int lvl = int.Parse(userData.Data_GetField(3).ToString());
                if (lvl == 5)
                {
                    mnuRegs.Enabled = true;
                    mnuPlanning.Enabled = true;
                    mnuAccount.Enabled = true;
                    mnuLookUp.Enabled = true;
                    mnuUserManager.Enabled = true;
                }
                if (lvl == 1) mnuRegs.Enabled = true;
                if (lvl == 2) mnuPlanning.Enabled = true;
                if (lvl == 3) mnuAccount.Enabled = true;
                
                mnuLogin.Text = "Logoff " + userData.Data_GetField(1);
            }
        }
    }
}
